package com.ezhuan.outer.controller;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ezhuan.common.controller.BaseController;
import com.ezhuan.common.util.ConstatFinalUtil;
import com.ezhuan.common.util.PageInfoUtil;
import com.ezhuan.task.pojo.ATask;
import com.ezhuan.task.pojo.ATaskEnum;
import com.ezhuan.task.service.ITaskService;
import com.ezhuan.task.service.impl.TaskServiceImpl;

/**
 * 任务相关的Controller
 * @author wangshSxt
 *
 */
public class TaskOuterController extends BaseController
{
	/* 系统相关的Service */
	private ITaskService taskService = new TaskServiceImpl();
	
	/**
	 * 任务列表
	 */
	public void taskList()
	{
		ConstatFinalUtil.LOGGER.info("======taskList======");
		long st = System.currentTimeMillis() ; 
		/* 存储结果的JSON */
		JSONObject resultJSON = new JSONObject();
		/* 接收参数 */
		String taskType = this.getPara("taskType");
		/* 分类Id,广告Id */
		String cateId = this.getPara("cateId");
		if (taskType == null)
		{
			taskType = "";
		}
		if (cateId == null)
		{
			cateId = "";
		}
		try
		{
			PageInfoUtil pageInfoUtil = this.proccedPageInfoUtil() ; 
			/* 查询数据库 */
			Map<String, Object> condMap = new HashMap<String, Object>();
			condMap.put("status", ATaskEnum.STATUS_ENABLE.getStatus());
			condMap.put("cateId", cateId);
			condMap.put("taskType", taskType);
			List<ATask> taskList = this.taskService.findCondListTaskService(pageInfoUtil, condMap);
			
			JSONArray taskArr = new JSONArray();
			for (Iterator iterator = taskList.iterator(); iterator.hasNext();)
			{
				ATask task = (ATask) iterator.next();
				taskArr.add(task.toJSON());
			}
			JSONObject dataJSON = new JSONObject();
			dataJSON.put("taskList", taskArr);
			dataJSON.put("pageInfo", pageInfoUtil.toJSON());
			resultJSON.put("data", dataJSON);
			
			/* 存储结果 */
			resultJSON.put("code", "0");
			resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("0"));
		} catch (Exception e)
		{
			ConstatFinalUtil.LOGGER.error("任务列表报错了:id:{}", e);
			resultJSON.put("code", "-1");
			resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("-1") + e.toString());
		}
		long ed = System.currentTimeMillis() ;
		resultJSON.put("consumeTime", (ed - st) + "毫秒");
		/* 封装结果 */
		renderJson(resultJSON);
		System.out.println(resultJSON);
	}
}
